<!--
 * @author  dumeng
专项答题
-->
<template>
  <section class="special-main">
    <el-collapse-transition>
      <section v-show="searchshow" class="main-search merge">
        <span class="svg-container">
          <svg-icon icon-class="search" />
        </span>
        <el-input v-model.trim="listQuery.name" placeholder="输入活动名称搜索" class="search-input" @keyup.enter.native="handleFilter" />
        <section class="search-btn">
          <el-button class="filter-item" type="primary" @click="handleFilter">搜索</el-button>
          <el-button type="primary" @click="handleReset">重置</el-button>
        </section>
      </section>
    </el-collapse-transition>

    <section class="main-buttons">
      <section class="main-buttons-title">
        <span>答题活动</span>
      </section>
      <section class="main-buttons-btns">
        <el-button style="margin-left: 10px;" @click="handleCreate">新建</el-button>
        <el-button style="margin-left: 10px;" @click="handleSelectPlatform">选取SaaS平台答题 </el-button>
        <el-button style="margin-left: 10px;" @click="handleDeletes">批量删除</el-button>
      </section>
    </section>

    <section class="main-table">
      <v-table
        :list="list"
        tag="newSpecial"
        :list-loading="listLoading"
        :source-arr="['imgIdUrl', 'imgIdUrl']"
        :cell-style="() => {}"
        :filter-column="filtercolumn"
        @handleSelectionChange="handleSelectionChange"
      >
        <el-table-column label="操作" fixed="right" align="center" min-width="100" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button v-if="scope.row.isForce !== 'SF01'" type="text" size="mini" @click="handleStatus(scope.row)">{{
              scope.row.status == 'ZT01' ? '禁用' : '启用'
            }}</el-button>
            <el-button v-if="scope.row.isForce !== 'SF01'" type="text" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
            <el-button v-if="scope.row.isForce !== 'SF01'" type="text" size="mini" @click="handleDelete(scope.row.id)">删除</el-button>
            <el-button type="text" size="mini" @click="handlePass(scope.row)">关口</el-button>
            <el-button
              v-if="scope.row.isForce === 'SF01'"
              type="text"
              size="mini"
              @click="handleSort(scope.row)"
            >修改排序</el-button>
          </template>
        </el-table-column>
      </v-table>

      <pagination v-show="total > 0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    </section>
    <el-dialog
      v-el-drag-dialog
      title="修改排序"
      width="30%"
      :visible.sync="sortDialog"
      @close="cancelDialog"
    >
      <el-input
        v-model="form.sort"
        placeholder="请输入排序值"
        type="number"
      ></el-input>
      <span slot="footer" class="dialog-footer">
        <el-button style="padding: 8px 30px;" @click="cancelDialog">取 消</el-button>
        <el-button style="padding: 8px 30px;" type="primary" @click="handelUpdateSort">确 定</el-button>
      </span>
    </el-dialog>
  </section>
</template>
<script>
import Pagination from '@/components/Pagination'
import VTable from '@/components/Table'
import VBank from '@/components/Bank'
import { queryPage, update, del, updateStatus } from '../api/newSpecial'
import permission from '@/directive/permission/index.js'
import elDragDialog from '@/directive/el-drag-dialog'
export default {
  name: 'SpecialMain',
  components: { Pagination, VTable, VBank },
  directives: { permission, elDragDialog },
  data() {
    return {
      listQuery: {
        page: 1,
        limit: 10,
        name: ''
      },
      searchshow: true,
      search: {
        name: '',
        deptIdValue: ''
      },
      list: [],
      total: 0,
      listLoading: false,
      multipleSelection: [],
      view: '/API-DOCUMENT/document/file/download?id=',
      vid: '',
      link: '/special/index?id=',
      form: {},
      sortDialog: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleSelectPlatform() {
      this.$router.push({ path: '/skill/special/SelectCourse4platform', name: 'SelectCourse4platformAnswerActivity', query: {}})
    },
    filtercolumn(scope, item) {
      if (item === 'questionIdValue') {
        return scope.row[item] ? scope.row[item] : '未设置'
      } else if (item === 'activityBackgroundColor') {
        return `<span style="display: inline-block;width: 40px; height: 40px;background-color: ${scope.row[item] || '无'}"></span>`
      } else if (item === 'imgIdUrl') {
        return scope.row['imgIdUrl']
      } else {
        return scope.row[item]
      }
    },

    handleSelectionChange(arr) {
      this.multipleSelection = arr
    },

    handleReset() {
      this.listQuery.page = 1
      this.listQuery.name = ''
      for (const i in this.search) {
        if (this.search['' + i + ''] instanceof Array) {
          this.search['' + i + ''] = []
        } else {
          this.search['' + i + ''] = ''
        }
      }
      this.getList()
    },

    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },

    handleCreate() {
      this.$router.push({ path: this.$filterPath('manage'), query: { type: 'c' }})
    },
    handlePass(item) {
      this.$router.push({ path: this.$filterPath('passList'), query: { i: item.id, relevanceId: item.relevanceId }})
    },
    handleUpdate(row) {
      this.$router.push({ path: this.$filterPath('manage'), query: { type: 'u', i: row.id }})
    },

    handleStatus(row) {
      const state = row.status === 'ZT01' ? 'ZT02' : 'ZT01'
      let msg = ''
      if (row.status === 'ZT01') {
        msg = '禁用'
      } else {
        msg = '启用'
      }
      this.$confirm('确定' + msg + '该专项答题活动吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          const loading = this.$loading({
            lock: true,
            text: '请稍后',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.3)'
          })
          return new Promise((resolve, reject) => {
            updateStatus({ id: row.id, status: state })
              .then(() => {
                loading.close()
                this.$message({
                  title: '成功',
                  message: msg + '成功',
                  type: 'success',
                  duration: 2000
                })
                this.getList()
              })
              .catch((error) => {
                loading.close()
                reject(error)
              })
          })
        })
        .catch(() => {})
    },

    handleDeletes() {
      const arr = []
      if (this.multipleSelection.length > 0) {
        this.multipleSelection.forEach((item) => {
          arr.push(item.id)
        })
        var ids = arr.join(',')
        this.$confirm('此操作将永久删除所选数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            return new Promise((resolve, reject) => {
              del(ids)
                .then(() => {
                  this.$message({
                    title: '成功',
                    message: '删除成功',
                    type: 'success',
                    duration: 2000
                  })
                  this.getList()
                })
                .catch((error) => {
                  reject(error)
                })
            })
          })
          .catch(() => {})
      } else {
        this.$message({
          message: '请选择需要删除的数据！',
          type: 'warning'
        })
      }
    },

    handleDelete(ids) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          return new Promise((resolve, reject) => {
            del(ids)
              .then(() => {
                this.$message({
                  title: '成功',
                  message: '删除成功',
                  type: 'success',
                  duration: 2000
                })
                this.getList()
              })
              .catch((error) => {
                reject(error)
              })
          })
        })
        .catch(() => {})
    },

    getList(obj) {
      this.listLoading = true
      var query = {
        page: obj ? obj.page : this.listQuery.page,
        pageSize: obj ? obj.limit : this.listQuery.limit
      }
      if (this.searchshow) {
        query['name'] = this.listQuery.name
      } else {
        query['name'] = this.search.name
        // query['orgName'] = this.search.orgName
      }
      return new Promise((resolve, reject) => {
        queryPage(query)
          .then((response) => {
            this.list = response.data.data.rows
            this.total = response.data.data.total
            this.listLoading = false
          })
          .catch((error) => {
            reject(error)
            this.listLoading = false
          })
      })
    },

    // 修改排序
    handelUpdateSort() {
      if (this.form.sort === '') {
        this.$message({
          message: '请输入排序值',
          type: 'warning'
        })
        return
      }
      const obj = Object.assign({}, this.form)

      const loading = this.$loading({
        lock: true,
        text: '请稍后',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.3)'
      })
      update(obj).then(res => {
        if (res.data.code === '200') {
          loading.close()
          this.sortDialog = false
          this.$message({
            message: '修改成功',
            type: 'success'
          })
          this.getList()
        } else {
          loading.close()
          this.$message({
            message: '修改失败',
            type: 'error'
          })
        }
      }).catch(() => {
        loading.close()
      })
    },

    // 强制下发数组修改排序
    handleSort(row) {
      const obj = Object.assign({}, row)
      obj.sort = ''
      this.form = obj
      this.sortDialog = true
    },

    // 取消修改排序
    cancelDialog() {
      this.sortDialog = false
      this.form = {}
    }
  }
}
</script>
<style lang="scss">
.special-main {
  padding: 10px;
  .main-search {
    background-color: #fff;
    padding: 0 10px;
    .svg-container {
      padding: 0 5px 0 10px;
      color: #bfced6;
      vertical-align: middle;
      display: inline-block;
    }
    .search-input {
      width: 60%;
      position: relative;
      &:after {
        content: '';
        position: absolute;
        left: -25px;
        right: 0;
        bottom: 8px;
        height: 1px;
        background: rgba(226, 227, 232, 1);
      }
      input {
        outline: none;
        background: transparent;
        border: none;
        outline: medium;
        padding: 0;
        &:focus {
          outline: none;
          background-color: transparent;
        }
      }
    }
    .search-btn {
      display: inline-block;
      float: right;
      .el-button.is-round {
        padding: 10px 30px;
        margin-right: 34px;
      }
      .search-more {
        font-size: 14px;
        font-weight: 500;
        @include font-color($mainColor);
        cursor: pointer;
        margin-left: 10px;
      }
    }

    &.merge {
      height: 50px;
      line-height: 50px;
    }

    &.separate {
      .search-inner {
        padding-top: 10px;
        .search-inner-item {
          .item-label {
            height: 30px;
            line-height: 30px;
            font-size: 14px;
            color: #a6a6a6;
            font-weight: 400;
          }
          .el-input {
            position: relative;
            &.el-date-editor input {
              padding-left: 30px;
            }
            .el-input__inner {
              border: none;
              padding: 0;
            }
            &:after {
              content: '';
              height: 1px;
              background-color: #e2e3e8;
              position: absolute;
              left: 0;
              right: 0;
              bottom: 0;
            }
          }
        }
      }
      .search-btn {
        text-align: center;
        display: block;
        float: none;
        height: 56px;
        line-height: 56px;
      }
    }
  }

  .main-buttons {
    display: flex;
    line-height: 36px;
    margin-top: 10px;
    .main-buttons-title {
      flex: 1;
      height: 36px;
      line-height: 36px;
      font-size: 16px;
      font-weight: bold;
      color: rgba(51, 51, 51, 1);
    }
  }
}
</style>
